<template>
  <div>
    <div class="query-form">
      <el-form ref="queryForm" :inline="true" :model="data_query_form.data">
        <el-form-item label="模板名称：" prop="name">
          <el-input v-model="data_query_form.data.name" type="text" placeholder="模板名称" />
        </el-form-item>
        <el-form-item>
          <el-button @click="queryFormReset">
            重置
          </el-button>
          <el-button type="primary" @click="queryFormQuery">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="template-list">
      <el-table
        border
        style="width: 100%"
        max-height="300"
        :data="data_template_list.data"
        :highlight-current-row="false"
        @selection-change="templateListSelectionChange"
      >
        <el-table-column align="center" label="选择" header-align="center" width="55">
          <template slot-scope="scope">
            <el-radio
              v-model="data_template_list.table.radio"
              :label="scope.row.id"
              @change.native="templateListSelectionChange(scope.$index, scope.row)"
            >
              <i />
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, index) in data_template_list.columns"
          :key="index"
          align="center"
          header-align="center"
          :type="item.type"
          :prop="item.prop"
          :label="item.label"
        />
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next, sizes, jumper"
          :total="data_template_list.pagination.total"
          :page-size="data_template_list.pagination.page_size"
          @current-change="templateListPaginationChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ApiMMallWeb } from '@services/api';

export default {
  name: 'ProductAddDialogOrderTemplateComponent',
  props: {
    query: {
      type: Object,
      default: function() {
        return {};
      }
    },
    fetcher: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      data_query_form: {
        data: {
          name: ''
        },
        ext_data: {
          name: ''
        }
      },
      data_template_list: {
        data: [],
        table: {
          radio: ''
        },
        columns: [
          {
            prop: 'number',
            label: '序号'
          },
          {
            prop: 'name',
            label: '模板名称'
          }
        ],
        pagination: {
          total: 0,
          page_size: 0
        }
      }
    };
  },
  computed: {
    apiMMallWeb: function() {
      return new ApiMMallWeb(this.fetcher);
    }
  },
  async created() {
    const result = await this.getProductListV1Api();
    this.data_template_list.data = result.content;
    this.data_template_list.pagination.total = result.totalElements;
    this.data_template_list.pagination.page_size = result.size;
  },
  methods: {
    queryFormReset() {
      this.$refs['queryForm'].resetFields();
    },
    templateListSelectionChange(index, selectedData) {
      this.$emit('selectionChange', selectedData);
    },
    templateListPaginationChange(currentIndex) {},
    queryFormQuery() {
      this.data_query_form.ext_data.name = this.data_query_form.data.name;
    },
    pullTemplateListData({ page, pageSize } = {}) {
      // eslint-disable-next-line no-param-reassign
      page || (page = 1);
      // eslint-disable-next-line no-param-reassign
      pageSize || (pageSize = 10);
      const query = this.query;
      return this.apiMMallWeb.OrderTemplateOrderV1ApiPOST({
        query: {
          operationType: query.operationType,
          page: page,
          pageSize: pageSize,
          templateName: this.data_query_form.ext_data.name
        }
      });
    },
    async getProductListV1Api(pager, params) {
      let result = await new Promise((resolve, reject) => {
        resolve({
          payload: {
            content: [
              {
                id: '1',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '2',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '3',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '4',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '5',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '6',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '7',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '8',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '9',
                number: '12345679',
                name: '王小虎'
              },
              {
                id: '10',
                number: '12345679',
                name: '王小虎'
              }
            ],
            number: 1,
            size: 20,
            totalPages: 2,
            totalElements: 24,
            numberOfElements: 20
          },
          code: '0',
          msg: 'ok'
        });
      });
      return result.payload;
    }
  }
};
</script>

<style lang="less" scoped>
.template-list {
  margin-top: 60px;
  .pagination {
    text-align: right;
    margin-top: 20px;
  }
}
</style>
